home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / tkern10.zip / SRC\MISC.C < prev    next >
Text File  |  1994-05-14  |  3KB  |  119 lines

  1. /*
  2.  *  This file forms part of "TKERN" - "Troy's Kernel for Windows".
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <windows.h>
  22. #include <toolhelp.h>
  23. #include <stdlib.h>
  24. #include <memory.h>
  25. #include <string.h>
  26. #include <alloc.h>
  27. #include <stdarg.h>
  28. #include <errno.h>
  29. #include <sys/tfile.h>
  30. #include <sys/task.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/wait.h>
  33. #include <sys/tkern.h>
  34.  
  35. int    nSleepers = 0;    // Number of sleeping tasks
  36.  
  37. /*
  38.  * We couldn't use PeekMessage because you can't seem to have 2 tkern tasks
  39.  * using PeekMessage loops at the same time. GetMessages puts the current task
  40.  * to sleep and marks it as sleeping. The file manager will issue a wakeup
  41.  * call when an event has occurred on which sleeping tasks are waiting.
  42.  * This will cause tkern to send a dummy message to all tasks which are sleeping.
  43.  * That will cause the GetMessage call to return
  44.  */
  45.  
  46. void    GetMessages(struct task *pt)
  47. {
  48.     MSG    msg;
  49.  
  50.     if (pt->nSignal)
  51.     {
  52.         (*pt->intrproc)(pt->nSignal);
  53.         pt->nSignal = 0;
  54.     }
  55.     nSleepers++;
  56.     pt->nFlags |= TF_ASLEEP;
  57.     GetMessage(&msg, 0, 0, 0);
  58.     if (msg.message != TKWM_WAKEUP)
  59.     {
  60.         TranslateMessage(&msg);
  61.         DispatchMessage(&msg);
  62.     }
  63.     if (pt->nFlags & TF_ASLEEP)
  64.     {
  65.         nSleepers--;
  66.         pt->nFlags &= ~TF_ASLEEP;
  67.     }
  68.     if (pt->nSignal)
  69.     {
  70.         (*pt->intrproc)(pt->nSignal);
  71.         pt->nSignal = 0;
  72.     }
  73. }
  74.  
  75. /* FlushMessages should be used only when there is no alternative */
  76. void    FlushMessages(void)
  77. {
  78.     MSG    msg;
  79.  
  80.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  81.     {
  82.         TranslateMessage(&msg);
  83.         DispatchMessage(&msg);
  84.     }
  85. }
  86.  
  87.  
  88. /*
  89.  * This function is used by tkern_exec() to copy the argument and
  90.  * environment vectors.
  91.  */
  92.  
  93. void
  94. Copy_Array(char ***dest, char **source)
  95. {
  96.     int    i;
  97.     int    nBytes = sizeof(char *);
  98.     char    *pchBuffer;
  99.     char    **ppchVector;
  100.  
  101.     for (i = 0; source[i]; i++)
  102.     {
  103.         nBytes += strlen(source[i]) + 1;
  104.     }
  105.     nBytes += (i + 1) * sizeof(char *);
  106.     ppchVector = (char **) malloc(nBytes);
  107.     *dest = ppchVector;
  108.     pchBuffer = (char *) (ppchVector + i + 1);
  109.     while (*source)
  110.     {
  111.         *ppchVector++ = pchBuffer;
  112.         strcpy(pchBuffer, *source);
  113.         pchBuffer += strlen(pchBuffer) + 1;
  114.         source++;
  115.     }
  116.     *ppchVector = 0;
  117. }
  118.  
  119.